package com.cat.dataStructure02;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/design-a-number-container-system/
 * @create 2025/10/30 17:08
 * @since JDK17
 */

public class Solution84 {
    Map<Integer, Integer> m1 = new HashMap<>();
    Map<Integer, TreeSet<Integer>> m2 = new HashMap<>();
    public void change(int index, int number) {
        Integer x = m1.getOrDefault(index, -1);
        if (x == -1) {  // 该位置不存在number
            m1.put(index, number);
            m2.computeIfAbsent(number, i -> new TreeSet<>()).add(index);
        } else {    // 改位置存在number
            m2.get(x).remove(index);
            m2.computeIfAbsent(number, y -> new TreeSet<>()).add(index);
        }
    }

    public int find(int number) {
        TreeSet<Integer> set = m2.get(number);
        return set == null || set.isEmpty() ? -1 : set.first();
    }
}
